/*================================================================
*   文件名称：main.cpp
*   创 建 者：yang qiang
*   创建日期：2019年06月30日
*   描    述：
*   
================================================================*/


#include <iostream>

using namespace std;

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if( head == NULL )
            return NULL;

        ListNode newhead(0);
        ListNode *p = head->next, *q = NULL;
        head->next = NULL;
        newhead.next = head;
        
        while( p ){
            q = p;
            p = p->next;
            q->next = newhead.next;
            newhead.next = q;
        }

        return newhead.next;
    }
};
